<template>
  <view class="move-container">
    <view class="form-section">

      <view class="form-item">
        <text class="label">地码
          <text class="required">*</text>
        </text>
        <input
            v-model="formData.fromLocation"
            class="input"
            type="text"
            placeholder="请扫描或输入地码"
        />
      </view>

      <view class="form-item">
        <text class="label">终点库位
          <text class="required">*</text>
        </text>
        <input
            v-model="formData.toLocation"
            class="input"
            type="text"
            placeholder="请扫描或输入终点库位"
        />
      </view>

      <view class="form-item">
        <text class="label">任务类型
          <text class="required">*</text>
        </text>
        <picker
            mode="selector"
            :range="taskType"
            range-key="label"
            @change="onBillChange"
            :value="billIndex">
          <view class="input">
            {{ taskType[billIndex] ? taskType[billIndex].label : '请选择任务类型' }}
          </view>
        </picker>
      </view>

      <view class="form-item">
        <text class="label">车辆
          <text class="required">*</text>
        </text>
        <!-- 修正 range-key 语法，移除可选链操作符 -->
        <picker
            mode="selector"
            :range="chooseAGV"
            range-key="label"
            @change="chooseAGVcar"
            :value="agvId">
          <view class="input">
            {{ chooseAGV[agvId] ? chooseAGV[agvId].label : '请选择车辆' }}
          </view>
        </picker>
      </view>

      <!-- <view class="form-item">
        <text class="label">备注</text>
        <input
            v-model="formData.remark"
            class="input"
            type="text"
            placeholder="请输入备注信息"
        />
      </view> -->

      <view class="btn-group">
        <button @click="handleReset" class="btn btn-default">重置</button>
        <button @click="handleSubmit" class="btn btn-primary" :disabled="loading">
          <text v-if="loading" class="cuIcon-loading2 iconfont-spin"></text>
          {{ loading ? '提交中...' : '确认移动' }}
        </button>
      </view>
    </view>
  </view>
</template>

<script>

import {ManualTask} from '@/api/system/stock';

var main, receiver, filter;

export default {
  data() {
    return {
      taskType: [
        {value: '2', label: '入库'},
        {value: '1', label: '出库'},
      ],
      chooseAGV:[],
      billIndex: null,
      agvId: 0,
      formData: {
        // instrock: '',
        taskType: '',
        fromLocation: '',
        agvId: '',
        toLocation: ''
        // remark: ''
      },
      loading: false
    };
  },

  onLoad() {
    this.initScan();
  },

  onShow() {
    this.startScan();
  },

  onHide() {
    this.stopScan();
  },

  onUnload() {
    this.stopScan();
  },

  methods: {
    onBillChange(e) {

      this.billIndex = e.detail.value;
      console.log("this.billIndex",this.billIndex)
      this.formData.taskType = this.taskType[this.billIndex].value;
    },
    chooseAGVcar(e) {
      this.agvId = e.detail.value;
      this.formData.chooseAGV = this.chooseAGV[this.agvId].value;
    },
    initScan() {
      let _this = this;
      /* #ifdef APP-PLUS */
      main = plus.android.runtimeMainActivity();
      var IntentFilter = plus.android.importClass('android.content.IntentFilter');
      filter = new IntentFilter();
      filter.addAction("com.service.scanner.data")
      receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
        onReceive: function (context, intent) {
          plus.android.importClass(intent);
          let code = intent.getStringExtra("ScanCode");
          console.log(code)
          _this.handleScanResult(code);
        }
      });
      /* #endif */
    },

    startScan() {
      /* #ifdef APP-PLUS */
      main.registerReceiver(receiver, filter);
      /* #endif */
    },

    stopScan() {
      /* #ifdef APP-PLUS */
      main.unregisterReceiver(receiver);
      /* #endif */
    },

    // 处理扫码结果
    handleScanResult(code) {
      // 判断扫码内容类型
      if (code.startsWith('L')) {
        // 库位格式
        if (!this.formData.fromLocation) {
          this.formData.fromLocation = code.substring(1);
        } else if (!this.formData.toLocation) {
          this.formData.toLocation = code.substring(1);
        }
      } else if (code.startsWith('M')) {
        // 物料编号格式
        this.formData.materialCode = code.substring(1);
      } else {
        // 默认当作物料编号处理
        this.formData.materialCode = code;
      }
    },

    // 重置表单
    handleReset() {
      this.formData = {
        fromLocation: '',
        toLocation: '',
        taskType: '',
        remark: ''
      };
      this.billIndex = null
    },

    // 提交表单
    handleSubmit() {
      if (this.loading) return;

      // 表单验证
      if (!this.formData.fromLocation) {
        this.$modal.msgError('请输入地码');
        return;
      }
      if (!this.formData.toLocation) {
        this.$modal.msgError('请输入终点库位');
        return;
      }
      if (!this.formData.taskType) {
        this.$modal.msgError('请输入任务类型');
        return;
      }
      this.loading = true;
      ManualTask(this.formData)
          .then(res => {
            console.log(res+"songmuyan")
            if (res.code === 200) {
              this.$modal.msgSuccess('手工任务成功');
              this.handleReset();
            } else {
              this.$modal.msgError(res.msg || '手工任务失败');
            }
          })
          .catch(error => {
            console.error('手工任务失败:', error);
            this.$modal.msgError('手工任务失败，请稍后重试');
          })
          .finally(() => {
            this.loading = false;
          });
    }
  }
}
</script>

<style lang="scss" scoped>
/* 样式保持不变 */
.move-container {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 16px;
}
.move-container {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 16px;
}

.form-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;

  .form-item {
    margin-bottom: 16px;

    .label {
      display: block;
      font-size: 14px;
      color: #333;
      margin-bottom: 8px;
      font-weight: 500;

      .required {
        color: #ff4d4f;
        margin-left: 4px;
      }
    }

    .input {
      width: 100%;
      height: 40px;
      background-color: #f8f9fa;
      border-radius: 4px;
      padding: 0 12px;
      font-size: 14px;
      color: #333;
    }
  }
}

.btn-group {
  display: flex;
  justify-content: space-between;
  margin-top: 24px;

  .btn {
    width: 48%;
    height: 40px;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 500;

    &.btn-default {
      background-color: #f8f9fa;
      color: #666;
    }

    &.btn-primary {
      background-color: #4A90E2;
      color: #fff;

      &:disabled {
        opacity: 0.7;
      }
    }
  }
}

.iconfont-spin {
  display: inline-block;
  animation: loading-rotate 2s linear infinite;
}

@keyframes loading-rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>